//
//  AlertCustomVC.m
//  AlumniBook
//
//  Created by 喂！ on 2018/10/31.
//  Copyright © 2018年 well. All rights reserved.
//

#import "AlertCustomVC.h"

@interface AlertCustomVC ()

@end

@implementation AlertCustomVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5f];
    [self initFrame];
}



-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if (self.hiddenCancel == false) {
        [self showAnimation];
    }
}


-(void)showAnimation
{
    [UIView setAnimationsEnabled:true];
    self.isAnimate = true;
    [UIView animateWithDuration:0.2 animations:^{
        self.cancelButton.transform = CGAffineTransformIdentity;
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        self.isAnimate = false;
    }];
}

-(void)initFrame
{

    [self.view addSubview:self.mainTableView];
    
    if (self.hiddenCancel == false) {
        [self.view addSubview:self.cancelButton];
        [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.mas_equalTo(self.view);
            make.height.mas_equalTo(50);
        }];

        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.text = @"发起事件:";
        [titleLabel setFont:[UIFont systemFontOfSize:36 * SCREEN_SCALE]];
        [titleLabel setTextColor:[UIColor whiteColor]];
        [self.view addSubview:titleLabel];
        
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_greaterThanOrEqualTo(self.view.mas_bottom).multipliedBy(286/667.0f);
            make.width.mas_equalTo(self.view).multipliedBy(554/750.0f);
            make.centerX.mas_equalTo(self.view);
        }];
        
        [self.mainTableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(titleLabel);
            make.top.mas_equalTo(titleLabel.mas_bottom).offset(5);
            make.bottom.mas_equalTo(self.cancelButton.mas_top);
            make.height.mas_equalTo(48 * self.dataArray.count).priorityMedium();
        }];
    }
    else
    {
        self.mainTableView.scrollEnabled = false;
        [self.mainTableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(self.view).multipliedBy(554/750.0f);
            make.centerX.mas_equalTo(self.view);
            make.bottom.mas_equalTo(self.view).offset(-50);
            make.height.mas_equalTo(self.dataArray.count * 48);
        }];
    }
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    UITouch *touch = [touches anyObject];
    UIView *view = (UIView *)[touch view];
    if (view == self.view) {
        [self closeView:nil];
    }
}



-(void)cancelClick
{
    [self closeView:nil];
}

/**
 关闭页面不进行创建
 */
-(void)closeView:(NSMutableDictionary *)dic
{
    if (self.isAnimate == true) {
        return;
    }
    
    self.isAnimate = true;
    
    [UIView animateWithDuration:0.2 animations:^{
        self.cancelButton.transform = CGAffineTransformMakeRotation(M_PI_4);
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        self.isAnimate = false;
        [self dismissViewControllerAnimated:false completion:^{
            self.changeBlock(dic);
        }];
    }];
}


#pragma mark - UITableViewDelegate and UITableDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:false];
    [self closeView:[self.dataArray objectAtIndex:indexPath.row]];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellId = @"AlertCustomVCCell";
    AlertCustomVCCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[AlertCustomVCCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    NSMutableDictionary *dic = [self.dataArray objectAtIndex:indexPath.row];
    [cell setCellText:[dic valueForKey:@"FunctionName"]];
    
    return cell;
}


#pragma mark    懒加载
-(UITableView *)mainTableView
{
    if (!_mainTableView) {
        _mainTableView = [UITableView new];
        _mainTableView = [[UITableView alloc] init];
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
        _mainTableView.estimatedRowHeight = 40;
        _mainTableView.rowHeight = UITableViewAutomaticDimension;
        _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainTableView.backgroundColor = [UIColor clearColor];
    }
    return _mainTableView;
}

-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

-(UIButton *)cancelButton
{
    if (!_cancelButton) {
        _cancelButton = [UIButton new];
        [_cancelButton addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
        [_cancelButton setImage:[UIImage imageNamed:@"CreateTaskChoosen"] forState:UIControlStateNormal];
        [_cancelButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
        _cancelButton.transform = CGAffineTransformMakeRotation(M_PI_4);
    }
    return _cancelButton;
}

@end
